﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using BUS;
using System.Globalization;

namespace QLPM
{
    public partial class frmTraCuuBenhNhan : DevComponents.DotNetBar.Office2007Form
    {
        public frmTraCuuBenhNhan()
        {
            InitializeComponent();
        }

        private void buttonThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        public void Load_MaBenhNhan()
        {
            BenhNhanBUS bnBus = new BenhNhanBUS();
            DataTable dt = new DataTable();
            dt = bnBus.LayBangBenhNhan();
            cmbMaBN.DataSource = dt;
            cmbMaBN.DisplayMember = "MaBenhNhan";
            cmbMaBN.ValueMember = "MaBenhNhan";
        }

        public void Load_TenBenhNhan()
        {
            BenhNhanBUS bnBus = new BenhNhanBUS();
            DataTable dt = new DataTable();
            dt = bnBus.LayBangBenhNhan();
            cmbTenBN.DataSource = dt;
            cmbTenBN.DisplayMember = "HoTen";
            cmbTenBN.ValueMember = "MaBenhNhan";
        }

        public void Load_TenLoaiBenh()
        {
            LoaiBenhBUS lbBus = new LoaiBenhBUS();
            DataTable dt = new DataTable();
            dt = lbBus.LayBangLoaiBenh();
            cmbTenLoaiBenh.DataSource = dt;
            cmbTenLoaiBenh.DisplayMember = "TenLoaiBenh";
            cmbTenLoaiBenh.ValueMember = "MaLoaiBenh";
        }

        public void Load_DiaChi()
        {
            BenhNhanBUS bnBus = new BenhNhanBUS();
            DataTable dt = new DataTable();
            dt = bnBus.LayBangBenhNhan();
            cmbDiaChi.DataSource = dt;
            cmbDiaChi.DisplayMember = "DiaChi";
            cmbDiaChi.ValueMember = "MaBenhNhan";
        }

        public void LoadBenhNhan()
        {
            BenhNhanBUS bnBus = new BenhNhanBUS();
            DataTable dt = new DataTable();
            dt = bnBus.LayBangBenhNhan();
            dt.Columns.Add("STT");
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                int j = i + 1;
                dt.Rows[i]["STT"] = j.ToString();
            }
            dtgvKetQuaTraCuu.DataSource = dt;
            dtgvKetQuaTraCuu.Columns["STT"].Width = 40;
            dtgvKetQuaTraCuu.Columns["STT"].DisplayIndex = 0;
            dtgvKetQuaTraCuu.Columns["STT"].ReadOnly = true;

        }

        private void frmTraCuuBenhNhan_Load(object sender, EventArgs e)
        {
            Load_MaBenhNhan();
            cbMaBN.Checked = true;
            dtpNgaySinhTu.Enabled = false;
            dtpNgaySinhDen.Enabled = false;
            dtpNgayKhamTu.Enabled = false;
            dtpNgayKhamDen.Enabled = false;
            radioNam.Enabled = false;
            radioNu.Enabled = false;
        }

        private void cbMaBN_CheckedChanged(object sender, EventArgs e)
        {
            if (cbMaBN.Checked == true)
            {
                cmbMaBN.Enabled = true;
                Load_MaBenhNhan();
            }
            else
                cmbMaBN.Enabled = false;
        }

        private void cbTenBN_CheckedChanged(object sender, EventArgs e)
        {
            if (cbTenBN.Checked == true)
            {
                cmbTenBN.Enabled = true;
                Load_TenBenhNhan();
            }
            else
                cmbTenBN.Enabled = false;
        }

        private void cbGioiTinh_CheckedChanged(object sender, EventArgs e)
        {
            if (cbGioiTinh.Checked == true)
            {
                radioNam.Checked = true;
                radioNam.Enabled = true;
                radioNu.Enabled = true;
            }
            else
            {
                radioNam.Enabled = false;
                radioNu.Enabled = false;
            }

        }

        private void cbDiaChi_CheckedChanged(object sender, EventArgs e)
        {
            if (cbDiaChi.Checked == true)
            {
                cmbDiaChi.Enabled = true;
                Load_DiaChi();
            }
            else
                cmbDiaChi.Enabled = false;
        }

        private void cbTenLoaiBenh_CheckedChanged(object sender, EventArgs e)
        {
            if (cbTenLoaiBenh.Checked == true)
            {
                cmbTenLoaiBenh.Enabled = true;
                Load_TenLoaiBenh();
            }
        }

        private void cbNgaySinh_CheckedChanged(object sender, EventArgs e)
        {
            if (cbNgaySinh.Checked == true)
            {
                dtpNgaySinhTu.Enabled = true;
                dtpNgaySinhDen.Enabled = true;
            }
        }

        private void cbNgayKham_CheckedChanged(object sender, EventArgs e)
        {
            if (cbNgayKham.Checked == true)
            {
                dtpNgayKhamTu.Enabled = true;
                dtpNgayKhamDen.Enabled = true;
            }
        }

        private void cbTatCa_CheckedChanged(object sender, EventArgs e)
        {
            if (cbTatCa.Checked == true)
                LoadBenhNhan();
        }

        public void HienThiSTT(DataTable dt)
        {
            dt.Columns.Add("STT");
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                int j = i + 1;
                dt.Rows[i]["STT"] = j.ToString();
            }
            dtgvKetQuaTraCuu.DataSource = dt;
            dtgvKetQuaTraCuu.Columns["STT"].Width = 35;
            dtgvKetQuaTraCuu.Columns["STT"].DisplayIndex = 0;
            dtgvKetQuaTraCuu.Columns["STT"].ReadOnly = true;
 
        }

        private void btnTraCuuBenhNhan_Click(object sender, EventArgs e)
        {
            BenhNhanBUS bnBus = new BenhNhanBUS();
            DataTable dt = new DataTable();

            if (cbMaBN.Checked == true)
            {
                string maBN;
                maBN = Convert.ToString(cmbMaBN.SelectedValue);

                dt = bnBus.TraCuuBenhNhanTheoMaBN(maBN);
                HienThiSTT(dt);
                dtgvKetQuaTraCuu.DataSource = dt;
            }

            if (cbTenLoaiBenh.Checked == true)
            {
                string maLoaiBenh;
                maLoaiBenh = Convert.ToString(cmbTenLoaiBenh.SelectedValue);

                dt = bnBus.TraCuuBenhNhanTheoLoaiBenh(maLoaiBenh);
                HienThiSTT(dt);
                dtgvKetQuaTraCuu.DataSource = dt;
            }
            if (cbTenBN.Checked == true)
            {
                string tenBenhNhan;
                tenBenhNhan = Convert.ToString(cmbTenBN.Text);

                dt = bnBus.TraCuuBenhNhanTheoTenBenhNhan(tenBenhNhan);
                HienThiSTT(dt);
                dtgvKetQuaTraCuu.DataSource = dt;
            }

            if (cbDiaChi.Checked == true)
            {
                string diaChi;
                diaChi = Convert.ToString(cmbDiaChi.Text);

                dt = bnBus.TraCuuBenhNhanTheoDiaChi(diaChi);
                HienThiSTT(dt);
                dtgvKetQuaTraCuu.DataSource = dt;
            }
            if (cbGioiTinh.Checked == true)
            {
                if (radioNam.Checked == true)
                {
                    dt = bnBus.TraCuuBenhNhanTheoGioiTinh("Nam");
                    HienThiSTT(dt);
                    dtgvKetQuaTraCuu.DataSource = dt;
                }
                if (radioNu.Checked == true)
                {
                    dt = bnBus.TraCuuBenhNhanTheoGioiTinh("Nữ");
                    HienThiSTT(dt);
                    dtgvKetQuaTraCuu.DataSource = dt;
                }
            }
            if (cbNgaySinh.Checked == true)
            {                
                //Chuyển định dạng từ "dd/MM/yyyy" thành "MM/dd/yyyy"
                string ngaySinhTu = dtpNgaySinhTu.Value.ToString("MM/dd/yyyy");
                string ngaySinhDen = dtpNgaySinhDen.Value.ToString("MM/dd/yyyy");

                dt = bnBus.TraCuuBenhNhanTheoNgaySinh(ngaySinhTu, ngaySinhDen);
                HienThiSTT(dt);
                dtgvKetQuaTraCuu.DataSource = dt;
            }

            if (cbNgayKham.Checked == true)
            {
                //Chuyển định dạng từ "dd/MM/yyyy" thành "MM/dd/yyyy"
                string ngayKhamTu = dtpNgayKhamTu.Value.ToString("MM/dd/yyyy");
                string ngayKhamDen = dtpNgayKhamDen.Value.ToString("MM/dd/yyyy");               
                dt = bnBus.TraCuuBenhNhanTheoNgayKham(ngayKhamTu, ngayKhamDen);
                HienThiSTT(dt);
                dtgvKetQuaTraCuu.DataSource = dt;
            }
        }
    }
}